package MyListNode;


/**
 * 链表反转
 */
public class Leetcode0206 {
//    public static ListNode reverseList(ListNode head) {
//        ListNode prev = null;
//        ListNode curr = head;
//        while (curr!=null){
//            ListNode after = curr.next;
//            curr.next = prev;
//            prev = curr;
//            curr = after;
//        }
//        return prev;
//    }

    public static ListNode reverseList(ListNode start) {
        ListNode pre = null;
        ListNode pos = start;

        while (pos != null){
            ListNode after = pos.next;
            pos.next = pre;
            pre = pos;
            pos = after;

        }
        return pre;
    }

    public static void main(String[] args) {
        ListNode a = new ListNode(1);
        ListNode b = new ListNode(2);
        ListNode c = new ListNode(3);

        a.next = b;
        b.next = c;
        c.next = null;

        ListNode cur = a;
        while (cur!=null){
            System.out.println(cur.val);
            cur = cur.next;
        }

        ListNode rev = reverseList(a);
        while (rev!=null){
            System.out.println(rev.val);
            rev = rev.next;
        }

    }
}
